//
//  IMViewController.m
//  QuantumWar
//
//  Created by mysj on 16/7/13.
//  Copyright © 2016年 zeng. All rights reserved.
//

#import "IMViewController.h"
#import "IMMessageBar.h"
#import "IMViewCell.h"
#import "Message.h"
#import <Masonry.h>
#import "IMTopView.h"
#import "UIView+Add.h"

const int messageBarH = 32.5;
static NSString *IMViewCellID = @"IMViewCellID";

@interface IMViewController () <UITableViewDelegate, UITableViewDataSource, IMMessageBarDelegate>

@property (nonatomic, strong) UITableView   *messageView;
@property (nonatomic, strong) IMMessageBar  *messageBar;
@property (nonatomic, strong) NSArray       *messgaes;
@property (nonatomic, strong) UIButton      *tp_infoBtn;
@property (nonatomic, strong) IMTopView     *imtpView;
@property (nonatomic, strong) UIView        *messageBarBack;
@property (nonatomic, strong) UIImageView   *im_viewbg;
///
@property (nonatomic, assign) CGFloat scope;
@end

@implementation IMViewController
#pragma mark - IMMessageBarDelegate
- (void)chatBar:(IMMessageBar *)chatBar sendMessage:(NSString *)message {

    NSLog(@"%@", message);
    Message *m = [Message new];
    m.m_userName = @"良辰";
    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.dateFormat = @"HH:mm";
    NSString *dataStr = [formatter stringFromDate:[NSDate date]];
    m.m_time = dataStr;
    m.isUser = YES;
    m.m_Text = message;
    
    NSMutableArray *mn = self.messgaes.mutableCopy;
    [mn addObject:m];
    self.messgaes = mn.copy;
    [self.messageView reloadData];
    NSUInteger sectionCount = [self.messageView numberOfSections];
    if (sectionCount) {
        
        NSUInteger rowCount = [self.messageView numberOfRowsInSection:0];
        if (rowCount) {
            
            NSUInteger ii[2] = {0, rowCount - 1};
            NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:ii length:2];
            [self.messageView scrollToRowAtIndexPath:indexPath
                                  atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
    
}

- (void)insertArray:(NSArray *)newAdditions atIndex:(NSUInteger)index {
    
    NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
    for(NSUInteger i = index; i < newAdditions.count + index; i++) {
        [indexes addIndex:i];
    }
    [self.messgaes.mutableCopy insertObjects:newAdditions atIndexes:indexes];
}

- (void)tp_infoBtnDidClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -
- (void)loadView {
    [super loadView];
    
    [self prepareView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

        NSMutableArray *marray = [NSMutableArray arrayWithCapacity:20];
        for (int i = 0; i <20; i++) {
            Message *m = [Message new];
            NSDateFormatter *formatter = [NSDateFormatter new];
            formatter.dateFormat = @"HH:mm";
            NSString *dataStr = [formatter stringFromDate:[NSDate date]];
            m.m_time = dataStr;
            m.m_userName = [NSString stringWithFormat:@"lkjlkljZ走%d",i];
            m.m_Text = @"索"
            "[开始讲道理]山东矿机阿喀琉斯几点啦口技刷卡了撒；打"
            "算离开到静安寺的骄傲了[开始讲道理]山东矿机阿喀琉斯"
            "几点离开大连；是的打算离开到静安寺的骄傲了[开始讲道理]"
            "山东矿机阿喀琉斯几点；拉克丝；拉克丝；跳转到[度]斯几点；拉克丝；拉克丝";
            [marray addObject:m];
        }
    
            self.messgaes = marray.copy;
            [self.messageView reloadData];
      
    
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.messgaes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    IMViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IMViewCellID forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[IMViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:IMViewCellID];
    }
    Message *model = self.messgaes[indexPath.row];
    cell.model = model;

//    [cell setNeedsUpdateConstraints];
//    [cell updateConstraintsIfNeeded];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Message *model = self.messgaes[indexPath.row];
    return model.cellH;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



#pragma mark - prepare
- (void)prepareView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.im_viewbg];
    [self.view addSubview:self.tp_infoBtn];
    [self.view addSubview:self.messageBarBack];
    [self.view addSubview:self.messageBar];
    [self.view addSubview:self.imtpView];
    [self.view addSubview:self.messageView];
    
    
    [self.tp_infoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@31);
        make.width.equalTo(@71);
    }];
    
    [self.im_viewbg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_infoBtn.mas_bottom);
        make.right.left.bottom.equalTo(self.view);
    }];
    
    [self.imtpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tp_infoBtn.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@120);
    }];
    
    [self.messageBarBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(messageBarH));
    }];
    
    [self.messageBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(messageBarH));
    }];
    
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imtpView.mas_bottom);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.messageBar.mas_top);
    }];
}

- (UIImageView *)im_viewbg {
    if (!_im_viewbg) {
        _im_viewbg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"im_View_bg"]];
    }
    return _im_viewbg;
}

- (IMTopView *)imtpView {
    if (!_imtpView) {
        _imtpView = [[IMTopView alloc] init];
        _imtpView.backgroundColor = [UIColor clearColor];
    }
    return _imtpView;
}

- (UIButton *)tp_infoBtn {
    if (!_tp_infoBtn) {
        _tp_infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tp_infoBtn setImage:[UIImage imageNamed:@"im_info_button_0"] forState:UIControlStateNormal];
        [_tp_infoBtn setImage:[UIImage imageNamed:@"im_info_button_0h"] forState:UIControlStateHighlighted];
        [_tp_infoBtn addTarget:self action:@selector(tp_infoBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _tp_infoBtn;
}

- (UIView *)messageBarBack {
    if (!_messageBarBack) {
        _messageBarBack = [UIView new];
    }
    return _messageBarBack;
}
- (IMMessageBar *)messageBar {
    if (!_messageBar) {
        _messageBar = [[IMMessageBar alloc] init];
        _messageBar.delegate = self;
        _messageBar.backgroundColor = [UIColor blackColor];
    }
    return _messageBar;
}

- (UITableView *)messageView {
    if (!_messageView) {
        _messageView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _messageView.backgroundColor = [UIColor clearColor];
        _messageView.dataSource = self;
        _messageView.delegate = self;
//        _messageView.estimatedRowHeight = 30;
        _messageView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_messageView registerClass:[IMViewCell class] forCellReuseIdentifier:IMViewCellID];
    }
    return _messageView;
}

@end
